package com.cheng.utils;

import com.cheng.dto.RowRenderCustomData;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.policy.DynamicTableRenderPolicy;
import com.deepoove.poi.policy.TableRenderPolicy;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import java.util.List;

/**
 * @author cheng-qiang
 * @date 2022年08月16日17:13
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class DynamicTableRenderCustomPolicy extends DynamicTableRenderPolicy {

    /**起始位置**/
    private Integer startRow;
    /**表格列数**/
    private Integer tableColumn;

    @Override
    public void render(XWPFTable xwpfTable, Object data) throws Exception {
        if (null==data){return;}
        RowRenderCustomData rowRenderCustomData = ((RowRenderCustomData) data);
        List<RowRenderData> renderDataList = rowRenderCustomData.getRowRenderData();
        if (CollectionUtils.isNotEmpty(renderDataList)&&renderDataList.size()!=0){
            xwpfTable.removeRow(startRow);
            for (int i = renderDataList.size() - 1; i >= 0; i--) {
                XWPFTableRow xwpfTableRow = xwpfTable.insertNewTableRow(startRow);
                for (int j = 0; j < tableColumn; j++) {
                    xwpfTableRow.createCell();
                }
                TableRenderPolicy.Helper.renderRow(xwpfTable.getRow(startRow), renderDataList.get(i));
            }
        }
    }

    public DynamicTableRenderCustomPolicy(Integer startRow,Integer tableColumn){
        this.startRow = startRow;
        this.tableColumn = tableColumn;
    }
}
